package com.it.flowable.service.leave.bpm;


import com.it.flowable.entity.leave.ActFlwServiceDataLeaveVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假流程线程处理
 */
public class BpmLeaveThreadLocalRe {
    private static ThreadLocal<List<ActFlwServiceDataLeaveVO>> actClmThreadLocalData = new ThreadLocal<List<ActFlwServiceDataLeaveVO>>();

    public static void bindData(List<ActFlwServiceDataLeaveVO> threadLocalData) {
        actClmThreadLocalData.set(threadLocalData);
    }

    public static void bindData() {
        actClmThreadLocalData.set(new ArrayList<>());
    }

    public static void unBindData() {
        actClmThreadLocalData.set(null);
    }

    public static void clear() {
        List<ActFlwServiceDataLeaveVO> threadLocalData = getThreadLocalDataAll();
        if (threadLocalData != null) {
            threadLocalData.clear();
        }
    }

    public static List<ActFlwServiceDataLeaveVO> getThreadLocalDataAll() {
        return actClmThreadLocalData.get();
    }

    public static void addThreadLocalData(ActFlwServiceDataLeaveVO taskExt) {
        List<ActFlwServiceDataLeaveVO> threadLocalData = actClmThreadLocalData.get();
        //修改流程图重新编译后actClmThreadLocalData.get()获取处理的对象是null 特需要重新绑定下变量信息
        if (threadLocalData != null) {
            threadLocalData.add(taskExt);
        } else {
            threadLocalData = new ArrayList<>();
            threadLocalData.add(taskExt);
            bindData(threadLocalData);
        }
    }

    // 处理要返回给业务系统的待办任务信息
    public static List<Map<String, Object>> getSessionData(List<ActFlwServiceDataLeaveVO> taskExtList) {
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        for (ActFlwServiceDataLeaveVO taskExt : taskExtList) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("cMainId", taskExt.getCMainId());
            dataMap.put("cTaskType", taskExt.getCTaskType());
            dataMap.put("cUserId", taskExt.getCUserId());
            dataMap.put("cTaskId", taskExt.getCTaskId());
//            dataMap.put("CProcessId", taskExt.getCProcessId());
            dataMap.put("CTaskNo", taskExt.getCTaskNo());
            dataList.add(dataMap);
        }
        return dataList;
    }
}
